﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SerbinAV.MobileSales.Sales.Entities;

namespace SerbinAV.MobileSales.Sales.DAL
{
    public class GraphicsVisits
    {
        private string _connectionString;

        public GraphicsVisits(string conectionString)
        {
            _connectionString = conectionString;
        }

        private List<BuyPointScheduler> LoadBuyPointScheduler(SqlCeCommand command)
        {
            List<BuyPointScheduler> buyPointSchedulers = new List<BuyPointScheduler>();
            using (SqlCeDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    BuyPointScheduler buyPointScheduler = EntityReader.ReadbuyPointScheduler(dr);
                    buyPointScheduler.BuyPoint = EntityReader.ReadBuyPoint(dr);
                    buyPointSchedulers.Add(buyPointScheduler);
                }
                return buyPointSchedulers;
            }
        }

        public List<BuyPointScheduler> GetAll()
        {
            using (SqlCeConnection connection = new SqlCeConnection(_connectionString))
            {
                string sqlQuery =
                    "SELECT * " +
                    " FROM GraphicsVisits g INNER JOIN BuyPoints AS b ON g.BuyPointId = b.IdBuyPoint";
                using (SqlCeCommand command = new SqlCeCommand(sqlQuery, connection))
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    return LoadBuyPointScheduler(command);
                }
            }
        }
    }
}
